//
//  MyHeaderView.m
//  Products_B
//
//  Created by lanou on 16/5/28.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "MyHeaderView.h"

@interface MyHeaderView()
@property (nonatomic, strong) UIButton *tempButton;
@property (nonatomic, assign) BOOL isCreat;
@property (nonatomic, assign) BOOL isClick;
@end

@implementation MyHeaderView

- (instancetype)initWithFrame:(CGRect)frame clickButtonBlock:(ClickButtonBlock)clickButtonBlack {
    _clickButtonBlock = clickButtonBlack;
    self = [super initWithFrame:frame];
    if (self) {
        _videoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kWidth * 662 / 1242)];
        _videoImageView.userInteractionEnabled = YES;
        [self addSubview:_videoImageView];
        
        _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _playButton.frame = CGRectMake((_videoImageView.frame.size.width - 40) / 2, (_videoImageView.frame.size.height - 40) / 2, 40, 40);
        [_playButton setImage:[UIImage imageNamed:@"CookClassPlay.png"] forState:UIControlStateNormal];
        [_playButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _playButton.tag = 110;
        [_videoImageView addSubview:_playButton];
        
        CGFloat x = 20;
        CGFloat y = _videoImageView.frame.origin.y + _videoImageView.frame.size.height + 10;
        _personLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, kWidth / 2, 20)];
        _personLabel.font = [UIFont systemFontOfSize:14];
        _personLabel.alpha = 0.5;
        [self addSubview:_personLabel];
        
        _collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _collectionButton.frame = CGRectMake(kWidth - 90, y, 25, 25);
        [_collectionButton setImage:[UIImage imageNamed:@"cookClassCollection.png"] forState:UIControlStateNormal];
        [_collectionButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _collectionButton.tag = 100;
        [self addSubview:_collectionButton];
        
        _shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _shareButton.frame = CGRectMake(kWidth - 50, y, 25, 25);
        [_shareButton setImage:[UIImage imageNamed:@"cookClassShare.png"] forState:UIControlStateNormal];
        [_shareButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _shareButton.tag = 102;
        [self addSubview:_shareButton];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, _personLabel.frame.origin.y + _personLabel.frame.size.height + 20, kWidth - 50, 20)];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:_titleLabel];
        
        // 该label需要动态返回高度，高度先随意设置
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 5, kWidth - 2 * x, 1)];
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.numberOfLines = 0;
        _contentLabel.alpha = 0.5;
        [self addSubview:_contentLabel];
        
        _downUpButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        _downUpButton1.frame = CGRectMake(kWidth - 50, _contentLabel.frame.origin.y - 20, 20, 20);
        [_downUpButton1 setImage:[UIImage imageNamed:@"cookClassShouqi.png"] forState:UIControlStateNormal];
        [_downUpButton1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _downUpButton1.tag = 103;
        [self addSubview:_downUpButton1];
        
        _xuanjiLabel = [[UILabel alloc] init];
        _xuanjiLabel.text = @"选集";
        [self addSubview:_xuanjiLabel];
        
        _updateLabel = [[UILabel alloc] init];
        _updateLabel.font = [UIFont systemFontOfSize:14];
        _updateLabel.alpha = 0.5;
        [self addSubview:_updateLabel];
        
        _downUpButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_downUpButton2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_downUpButton2 setImage:[UIImage imageNamed:@"cookClassShouqi.png"] forState:UIControlStateNormal];
        _downUpButton2.tag = 104;
        [self addSubview:_downUpButton2];

        _likeNumLabel = [[UILabel alloc] init];
        _likeNumLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_likeNumLabel];
        
        _likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_likeButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_likeButton setImage:[UIImage imageNamed:@"CookClassLike.png"] forState:UIControlStateNormal];
        _likeButton.tag = 105;
        [self addSubview:_likeButton];
        
        _buttonView = [[UIView alloc] init];
        [self addSubview:_buttonView];
        
        _buttonView2 = [[UIView alloc] init];
        _buttonView2.hidden = YES;
        [self addSubview:_buttonView2];
        
        _abouClass = [[UILabel alloc] init];
        _abouClass.text = @"相关课程";
        _abouClass.font = [UIFont systemFontOfSize:14];
        [self addSubview:_abouClass];
        
        _jinruButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _jinruButton.frame = CGRectMake(kWidth - 45, _abouClass.frame.origin.y, 25, 25);
        [_jinruButton setImage:[UIImage imageNamed:@"CookClassJinru.png"] forState:UIControlStateNormal];
        [_jinruButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _jinruButton.tag = 106;
        [self addSubview:_jinruButton];
        
    }
    return self;
}

- (void)layoutMySubViews {
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
    CGRect bounds = [_contentLabel.text boundingRectWithSize:CGSizeMake(_contentLabel.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    CGRect frame = _contentLabel.frame;
    frame.size.height = bounds.size.height;
    _contentLabel.frame = frame;
    
    [self layoutMySubViewsOfContentLabel];
}

// 布局contentLabel以下的子控件,之所以写成一个方法，是因为在外界需要单独调用
- (void)layoutMySubViewsOfContentLabel {
    _xuanjiLabel.frame = CGRectMake(20, _contentLabel.frame.origin.y + _contentLabel.frame.size.height + 10, 60, 30);
    _updateLabel.frame = CGRectMake(kWidth - 150, _xuanjiLabel.frame.origin.y + 2, 100, 20);
    _downUpButton2.frame = CGRectMake(kWidth - 50, _xuanjiLabel.frame.origin.y + 2, 20, 20);
    
    if (_buttonView2.hidden == NO) {
        _buttonView.frame = CGRectMake(20, _xuanjiLabel.frame.origin.y + _xuanjiLabel.frame.size.height + 15, kWidth - 40, (_upDateToClassNumber / 8 + 1) * (kWidth - 40 - 35) / 8 + (_upDateToClassNumber / 8) * 5);
    } else {
        self.buttonView.frame = CGRectMake(20, _xuanjiLabel.frame.origin.y + _xuanjiLabel.frame.size.height + 15, kWidth - 40, (kWidth - 40 - 35) / 8 * 2 + 10);
    }
    _buttonView2.frame = CGRectMake(0, ((kWidth - 40 - 35) / 8 * 2 + 10), kWidth - 40, (_upDateToClassNumber / 8 + 1) * (kWidth - 40 - 35) / 8 + (_upDateToClassNumber / 8) * 5 - ((kWidth - 40 - 35) / 8 * 2 + 5));
    [_buttonView addSubview:_buttonView2];
 
    [self layoutMySubViewsOfButton];
}

- (void)layoutMySubViewsOfButton {
    if (!_isCreat) {
        if (_upDateToClassNumber > 8) {
            NSInteger count = 1;
            // 课程每一集的Button
            for (int j = 1; j <= 2; j++) {
                for (int i = 1; i < 9; i++) {
                    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                    button.frame = CGRectMake(((kWidth - 40 - 35) / 8 + 5) * (i - 1),((kWidth - 40 - 35) / 8 + 5) * (j - 1), (kWidth - 40 - 35) / 8, (kWidth - 40 - 35) / 8);
                    button.backgroundColor = [UIColor grayColor];
                    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    [button setTitle:[NSString stringWithFormat:@"%ld",count] forState:UIControlStateNormal];
                    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
                    button.tag = 199 + count;
                    [self.buttonView addSubview:button];
                    
                    // 遍历完之后，取得最后一个button
                    self.tempButton = button;
                    if (count == _upDateToClassNumber) {
                        button.backgroundColor = [UIColor orangeColor];
                    }
                    count ++;
                }
            }
            for (int j = 1; j <= (_upDateToClassNumber / 8 - 2) + 1; j++) {
                for (int i = 1; i < 9; i++) {
                    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                    button.frame = CGRectMake(((kWidth - 40 - 35) / 8 + 5) * (i - 1),((kWidth - 40 - 35) / 8 + 5) * (j - 1), (kWidth - 40 - 35) / 8, (kWidth - 40 - 35) / 8);
                    button.backgroundColor = [UIColor grayColor];
                    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    [button setTitle:[NSString stringWithFormat:@"%ld",count] forState:UIControlStateNormal];
                    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
                    button.tag = 199 + count;
                    [self.buttonView2 addSubview:button];
                    
                    // 遍历完之后，取得最后一个button
                    self.tempButton = button;
                    if (count == _upDateToClassNumber) {
                        button.backgroundColor = [UIColor orangeColor];
                    }
                    count ++;
                    
                    if (count > _upDateToClassNumber) {
                        break;
                    }
                }
                if (count > _upDateToClassNumber) {
                    break;
                }
            }
            if (count - 1 <= 16) {
                self.downUpButton2.hidden = YES;
            }
        } else {
            for (int i = 1; i < _upDateToClassNumber + 1; i++) {
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(((kWidth - 40 - 35) / 8 + 5) * (i - 1), 0, (kWidth - 40 - 35) / 8, (kWidth - 40 - 35) / 8);
                button.backgroundColor = [UIColor colorWithRed:195 / 255.0 green:195 / 255.0 blue:195 / 255.0 alpha:1];
                [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
                button.tag = 199+i;
                [button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [self.buttonView addSubview:button];
                self.tempButton = button;
                if (i == _upDateToClassNumber) {
                    button.backgroundColor = [UIColor orangeColor];
                }
                // 如果更新的课程数小于或等于8，则隐藏上拉收起按钮
                self.downUpButton2.hidden = YES;
            }
        }
        
    }

    // 多少人觉得很赞
    _likeNumLabel.frame = CGRectMake(20, _buttonView.frame.origin.y + _buttonView.frame.size.height + 10, _tempButton.frame.size.width * 4 + 35, 20);
    // 赞
    _likeButton.frame = CGRectMake(_likeNumLabel.frame.origin.x + _likeNumLabel.frame.size.width + 30, _likeNumLabel.frame.origin.y, 20, 20);
    
    _abouClass.frame = CGRectMake(20, _likeNumLabel.frame.origin.y + _likeNumLabel.frame.size.height + 10, 200, 20);
    
    _jinruButton.frame = CGRectMake(kWidth - 45, _abouClass.frame.origin.y, 25, 25);
    
    _isCreat = YES;
}

// 点击button均触发的方法
- (void)buttonAction:(UIButton *)button {
    if (self.clickButtonBlock) {
        // 回调block，并将button当作参数传出去
        self.clickButtonBlock(button);
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
